package com.wgchao.identityrightsmanagement.infrastructure.dao;

import com.wgchao.identityrightsmanagement.domain.organization.model.Agency;
import com.wgchao.identityrightsmanagement.domain.organization.repository.AgencyRepository;
import com.wgchao.identityrightsmanagement.infrastructure.convert.AgencyConvert;
import com.wgchao.identityrightsmanagement.infrastructure.exception.ObjectExistException;
import com.wgchao.identityrightsmanagement.infrastructure.exception.ObjectNotExistException;
import com.wgchao.identityrightsmanagement.infrastructure.jpa.entity.SysBaseAgency;
import com.wgchao.identityrightsmanagement.infrastructure.jpa.repository.SysBaseAgencyRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @ClassName: AgencyDao
 * @author: wgchao
 * @createTime: 2020/6/11 4:12 PM
 * @Description:
 */
@Component
public class AgencyDao implements AgencyRepository {
    @Autowired
    private SysBaseAgencyRepository sysBaseAgencyRepository;
    @Autowired
    private AgencyConvert agencyConvert;

    @Override
    public String generateCode() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        return sdf.format(new Date())+UUID.randomUUID().toString().split("-")[1].toUpperCase();
    }

    @Override
    public Agency findById(String s) {
        SysBaseAgency sysBaseAgency = sysBaseAgencyRepository.findFirstByAgencyCodeAndDelFalse(s);
        if(sysBaseAgency == null){
            return null;
        }
        return agencyConvert.convert(sysBaseAgency);
    }

    @Override
    public void remove(Agency agency, String operator) {
        SysBaseAgency sysBaseAgency = sysBaseAgencyRepository.findFirstByAgencyCodeAndDelFalse(agency.getAgencyCode());
        sysBaseAgency.setDel(true);
        sysBaseAgency.setLastUpdator(operator);
        sysBaseAgency.setLastUpdateTime(new Date());
        sysBaseAgency = sysBaseAgencyRepository.save(sysBaseAgency);
    }

    @Override
    public void add(Agency agency, String operator) {
        if(agency.getId().compareTo(0L) < 0){//新增
            SysBaseAgency sysBaseAgency = sysBaseAgencyRepository.findFirstByAgencyCodeAndDelFalse(agency.getAgencyCode());
            if(sysBaseAgency != null){
                throw new ObjectExistException("机构编号已经被使用");
            }
            sysBaseAgency = agencyConvert.convert(agency);
            sysBaseAgency.setId(null);
            sysBaseAgency.setCreateTime(new Date());
            sysBaseAgency.setCreator(operator);
            sysBaseAgency.setDel(false);
            sysBaseAgency = sysBaseAgencyRepository.save(sysBaseAgency);
        }else{
            //修改
            SysBaseAgency sysBaseAgency = sysBaseAgencyRepository.findFirstByAgencyCodeAndDelFalse(agency.getAgencyCode());
            if(sysBaseAgency == null){
                throw new ObjectNotExistException("机构不存在");
            }
            SysBaseAgency sysBaseAgency_new = agencyConvert.convert(agency);
            sysBaseAgency_new.setLastUpdateTime(new Date());
            sysBaseAgency_new.setLastUpdator(operator);
            sysBaseAgency_new.setDel(sysBaseAgency.getDel());
            sysBaseAgency_new.setCreator(sysBaseAgency.getCreator());
            sysBaseAgency_new.setCreateTime(sysBaseAgency.getCreateTime());
            sysBaseAgency_new = sysBaseAgencyRepository.save(sysBaseAgency_new);
        }
    }

    @Override
    public void addAll(List<Agency> agencies, String operator) {
        for(Agency agency : agencies) {
            add(agency, operator);
        }
    }

    @Override
    public void removeAll(List<Agency> agencies, String operator) {
        for(Agency agency : agencies) {
            remove(agency, operator);
        }
    }
}
